<?php


namespace App\Http\Controllers;


use App\Models\Option;
use App\Models\Pet;
use App\Models\ServicePersonnel;
use App\Models\ServiceProject;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class PetsController extends Controller
{

    /**
     * 获取宠物配置
     */
    public function getConfig()
    {
        $type=request()->query('type',1);

//        $option_value = Option::get_pets_config();
        return Json(1, 'ok', ['breed' => $type==1?[
            ['name'=>'英短','price'=>50],
            ['name'=>'美短','price'=>50],
            ['name'=>'中华田园','price'=>50],
            ['name'=>'布偶','price'=>65],
            ['name'=>'暹罗','price'=>65],
            ['name'=>'缅因','price'=>65],
            ['name'=>'斯芬克斯无毛','price'=>65],
            ['name'=>'加菲','price'=>65],
            ['name'=>'德文','price'=>65],
            ['name'=>'阿比西尼亚','price'=>65],
            ['name'=>'其他品种','price'=>65],
        ]:[
            ['name'=>'超小型犬（4公斤以下）','price'=>60],
            ['name'=>'小型犬（4-10公斤）','price'=>60],
            ['name'=>'中型犬（11-30公斤）','price'=>70],
            ['name'=>'大型犬（31-40公斤）','price'=>80],
            ['name'=>'超大型犬（40公斤以上）','price'=>90],
        ], 'health' => [
            '已接种狂犬疫苗',
            '已接种猫瘟、猫杯状病毒、猫鼻疫苗（猫三联）',
            '均未接种',
        ]]);
    }

    public function add()
    {
        $data = request()->post();
        $id=isset($data['id'])?$data['id']:0;
        $validator = Validator::make($data, [
            'name' => [
                'required',
                'max:50'
            ],
        ], [
            'name.required' => '',
            'name.max' => '名字不能超过50个字符',
        ]);
        if ($validator->fails()) {
            return Json(0, $validator->errors()->first());
        }
        if(!empty($id)){
            $pet = Pet::query()->where('id','=',$data['id'])->first();
        }else{
            $pet = new Pet();
            $pet->uid = Auth()->id();

        }
        $exists=Pet::query()->where('uid','=',Auth()->id())->where('name',$data['name'])
            ->where(function ($query)use($id){
                if(!empty($id))  $query->where('id','<>',$id);
            })->exists();

        if ($exists) {
            return Json(0, "已存在相同名字");
        }

        $pet->name = $data['name'];
        $pet->type = $data['type'];
        $pet->breed = $data['breed'];
        $pet->sex = $data['sex'];
        $pet->age = $data['age'];
        $pet->health = explode(',', $data['healths']);
        $pet->album = explode(',', $data['files']);
        $pet->save();
        return Json(1, 'ok');
    }

    public function lists()
    {
        $data = Pet::query()->where('uid', '=', auth()->id())
            ->where(function ($query){
                $type=request()->query('type');
                    if(!empty($type)){
                        $query ->where('type','=',$type);
                    }
            })

            ->where('state', '=', 1)->get();
        $serviceProject = ServiceProject::query()->get();
        $servicePersonnel = ServicePersonnel::query()->orderBy('service_count','asc')->where('state', '=', 1)->get();
        $type=request()->query('type',1);
        return Json(1, 'ok', [
            'pets' => $data,
            'serviceProject' => $serviceProject,
            'handovers' => ['密码锁', '快递柜', '其它'],
            'servicePersonnel' => $servicePersonnel,
            'serviceSignPrice' => $type==1?[
                [
                    'type' => 1,
                    'label' => '上门费',
                    'money' => 25,
                ],
                [
                    'type' => 2,
                    'label' => '服务费用',
                    'money' => 25,
                ],
                [
                    'type' => 3,
                    'label' => '宠物数量（每增加一只加5元）',
                    'money' => 5,
                ],
                [
                    'type' => 10,
                    'label' => '服务天数',
                    'money' => 0,
                ],
            ]:[
                [
                    'type' => 1,
                    'label' => '上门费',
                    'money' => 25,
                ],
                [
                    'type' => 2,
                    'label' => '服务费用',
                    'money' => 35,
                ],
                [
                    'type' => 3,
                    'label' => '宠物数量（每增加一只加15元）',
                    'money' => 15,
                ],
                [
                    'type' => 10,
                    'label' => '服务天数',
                    'money' => 0,
                ],
            ]
        ]);
    }


    public function delete($id){
        Pet::query()->where('uid', '=', auth()->id())->where('id','=',$id)->delete();
        return Json(1, 'ok');
    }


}
